using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Components.Security
{
    public partial class User
    {
        #region Private members.

        protected int _ID;
        protected int _PortalID;
        protected string _Name = string.Empty;
        protected string _Password = string.Empty;
        protected string _Email = string.Empty;
        protected bool _Gender;
        protected string _Address = string.Empty;
        protected string _City = string.Empty;
        protected string _Country = string.Empty;
        protected string _Phone = string.Empty;
        protected string _AvatarImagePath = string.Empty;
        protected string _Status = string.Empty;
        protected DateTime _JoinedTime;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int PortalID
        {
            set { this._PortalID = value; }
            get { return this._PortalID; }
        }

        public string Name
        {
            set { this._Name = value; }
            get { return this._Name; }
        }

        public string Password
        {
            set { this._Password = value; }
            get { return this._Password; }
        }

        public string Email
        {
            set { this._Email = value; }
            get { return this._Email; }
        }

        public bool Gender
        {
            set { this._Gender = value; }
            get { return this._Gender; }
        }

        public string Address
        {
            set { this._Address = value; }
            get { return this._Address; }
        }

        public string City
        {
            set { this._City = value; }
            get { return this._City; }
        }

        public string Country
        {
            set { this._Country = value; }
            get { return this._Country; }
        }

        public string Phone
        {
            set { this._Phone = value; }
            get { return this._Phone; }
        }

        public string AvatarImagePath
        {
            set { this._AvatarImagePath = value; }
            get { return this._AvatarImagePath; }
        }

        public string Status
        {
            set { this._Status = value; }
            get { return this._Status; }
        }

        public DateTime JoinedTime
        {
            set { this._JoinedTime = value; }
            get { return this._JoinedTime; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static User Load(int iD)
        {
            string spName = "[dbo].p_SYSTEM_User_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            User entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new User();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PortalID"))) entity.PortalID = reader.GetInt32(reader.GetOrdinal("PortalID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) entity.Name = reader.GetString(reader.GetOrdinal("Name"));
                if (!reader.IsDBNull(reader.GetOrdinal("Password"))) entity.Password = reader.GetString(reader.GetOrdinal("Password"));
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
                if (!reader.IsDBNull(reader.GetOrdinal("Gender"))) entity.Gender = reader.GetBoolean(reader.GetOrdinal("Gender"));
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) entity.Address = reader.GetString(reader.GetOrdinal("Address"));
                if (!reader.IsDBNull(reader.GetOrdinal("City"))) entity.City = reader.GetString(reader.GetOrdinal("City"));
                if (!reader.IsDBNull(reader.GetOrdinal("Country"))) entity.Country = reader.GetString(reader.GetOrdinal("Country"));
                if (!reader.IsDBNull(reader.GetOrdinal("Phone"))) entity.Phone = reader.GetString(reader.GetOrdinal("Phone"));
                if (!reader.IsDBNull(reader.GetOrdinal("AvatarImagePath"))) entity.AvatarImagePath = reader.GetString(reader.GetOrdinal("AvatarImagePath"));
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetString(reader.GetOrdinal("Status"));
                if (!reader.IsDBNull(reader.GetOrdinal("JoinedTime"))) entity.JoinedTime = reader.GetDateTime(reader.GetOrdinal("JoinedTime"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------


        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_SYSTEM_User_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_SYSTEM_User_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_SYSTEM_User_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_SYSTEM_User_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(int portalID, string name, string password, string email, bool gender, string address, string city, string country, string phone, string avatarImagePath, string status, DateTime joinedTime)
        {
            User entity = new User();
            entity.PortalID = portalID;
            entity.Name = name;
            entity.Password = password;
            entity.Email = email;
            entity.Gender = gender;
            entity.Address = address;
            entity.City = city;
            entity.Country = country;
            entity.Phone = phone;
            entity.AvatarImagePath = avatarImagePath;
            entity.Status = status;
            entity.JoinedTime = joinedTime;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_SYSTEM_User_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@PortalID", SqlDbType.Int, this._PortalID);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@Password", SqlDbType.NVarChar, this._Password);
            db.AddInParameter(dbCommand, "@Email", SqlDbType.NVarChar, this._Email);
            db.AddInParameter(dbCommand, "@Gender", SqlDbType.Bit, this._Gender);
            db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, this._Address);
            db.AddInParameter(dbCommand, "@City", SqlDbType.NVarChar, this._City);
            db.AddInParameter(dbCommand, "@Country", SqlDbType.VarChar, this._Country);
            db.AddInParameter(dbCommand, "@Phone", SqlDbType.NVarChar, this._Phone);
            db.AddInParameter(dbCommand, "@AvatarImagePath", SqlDbType.NVarChar, this._AvatarImagePath);
            db.AddInParameter(dbCommand, "@Status", SqlDbType.VarChar, this._Status);
            db.AddInParameter(dbCommand, "@JoinedTime", SqlDbType.DateTime, this._JoinedTime);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int portalID, string name, string password, string email, bool gender, string address, string city, string country, string phone, string avatarImagePath, string status, DateTime joinedTime)
        {
            User entity = new User();
            entity.ID = iD;
            entity.PortalID = portalID;
            entity.Name = name;
            entity.Password = password;
            entity.Email = email;
            entity.Gender = gender;
            entity.Address = address;
            entity.City = city;
            entity.Country = country;
            entity.Phone = phone;
            entity.AvatarImagePath = avatarImagePath;
            entity.Status = status;
            entity.JoinedTime = joinedTime;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_SYSTEM_User_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@PortalID", SqlDbType.Int, this._PortalID);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@Password", SqlDbType.NVarChar, this._Password);
            db.AddInParameter(dbCommand, "@Email", SqlDbType.NVarChar, this._Email);
            db.AddInParameter(dbCommand, "@Gender", SqlDbType.Bit, this._Gender);
            db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, this._Address);
            db.AddInParameter(dbCommand, "@City", SqlDbType.NVarChar, this._City);
            db.AddInParameter(dbCommand, "@Country", SqlDbType.VarChar, this._Country);
            db.AddInParameter(dbCommand, "@Phone", SqlDbType.NVarChar, this._Phone);
            db.AddInParameter(dbCommand, "@AvatarImagePath", SqlDbType.NVarChar, this._AvatarImagePath);
            db.AddInParameter(dbCommand, "@Status", SqlDbType.VarChar, this._Status);
            db.AddInParameter(dbCommand, "@JoinedTime", SqlDbType.DateTime, this._JoinedTime);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            User entity = new User();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_SYSTEM_User_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}